package com.example.nfcschool;

import java.lang.reflect.Method;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.media.AudioManager;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.android.internal.telephony.ITelephony;

public class CallStateListener extends PhoneStateListener{
	
	public static final String TAG = "CallStateListner";
	private static int previousState = TelephonyManager.CALL_STATE_IDLE;
	private ITelephony telephonyService;
	private Context mContext;
	private static boolean mIsStartMain = false;//static �� ���������μ� ���� ���� ���� �� �� �ִ�. !!
	
	public CallStateListener(Context context) {
		mContext = context;
	}
	public void setIsReservedMainActivity(boolean setIs){
		Log.d(TAG, "setIsReservedMainActivity() setIs : "+setIs);
		mIsStartMain = setIs;
	}
	
	public int getRingMode() {
		AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
	    String message = null;
	    int ringMode = AudioManager.RINGER_MODE_VIBRATE;
	    
	    ringMode = audioManager.getRingerMode();
	    switch (audioManager.getRingerMode()) {
	    case AudioManager.RINGER_MODE_NORMAL:
	        message = "���Ҹ� ����Դϴ�";
	        break;
	    case AudioManager.RINGER_MODE_SILENT:
	    	ringMode = AudioManager.RINGER_MODE_SILENT;
	        message = "���� ����Դϴ�";
	        break;
	    case AudioManager.RINGER_MODE_VIBRATE:
	    	ringMode = AudioManager.RINGER_MODE_VIBRATE;
	        message = "���� ����Դϴ�";
	        break;
	    }
	    
	    //set the mode value 
	    ringMode = AudioManager.RINGER_MODE_VIBRATE;
	    
	    Log.d(TAG, "getRingMode() ringMode : "+ringMode);
	    
	    return ringMode;
	}
	
	public void setRingMode(int ringmode) {
		AudioManager audioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setRingerMode(ringmode);
	}
	
	public static boolean isRunningProcess(Context context, String packageName) {
		 
        boolean isRunning = false;
 
        ActivityManager actMng = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);                      
  
        List<RunningAppProcessInfo> list = actMng.getRunningAppProcesses();     
 
        for(RunningAppProcessInfo rap : list)                           
        {                                
            if(rap.processName.equals(packageName))                              
            {                                   
                isRunning = true;     
                break;
            }                         
        }
 
        return isRunning;
    }

	
	@Override
	public void onCallStateChanged(int state, String incomingNumber) {

		Log.d(TAG, "onCallStateChanged() mIsStartMain : "+mIsStartMain);
		if(!mIsStartMain)
		{
			Log.d(TAG, "onCallStateChanged() END");
			return;
		}
		
		//mReservedMain.getRingMode(AudioManager.RINGER_MODE_VIBRATE);
		// TODO Auto-generated method stub
		switch(state) {
		case TelephonyManager.CALL_STATE_IDLE:
			Log.d(TAG,"CALL_IDLE");
			break;
		case TelephonyManager.CALL_STATE_OFFHOOK:
			// ��ȭ���� ���� (��ȭ��ư�� ���� ����)
			Log.d(TAG, "CALL_OFFHOOK");
			break;
		case TelephonyManager.CALL_STATE_RINGING:
			//��ȭ���� �︮�� �ִ� ����
			
			Log.d(TAG, "CALL_RINGING");
			
			int ringMode = getRingMode();
			
			Log.d(TAG, "RINGING >> ringMode : " + ringMode);
			
			setRingMode(1);
			
	            
			Log.d(TAG, "RINGING >> asdasdasdaIncoming number : " + incomingNumber);
			
			Toast.makeText(mContext, "��ȭ�� �Ծ��  = " +incomingNumber, Toast.LENGTH_SHORT);
			
			
			
		}
		super.onCallStateChanged(state, incomingNumber);
	}
	
}
